package cn.xfdzx.android.apps.shop.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.AddAddressActivity;
import cn.xfdzx.android.apps.shop.bean.AddressListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.ConfirmRetailPresenter;
import cn.xfdzx.android.apps.shop.util.ShopCarPopupWindow;
import cn.xfdzx.android.apps.shop.util.Utils;

import static cn.xfdzx.android.apps.shop.apps.Constants.START_CODE;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：零售选地址
 */
public class SelectAddressRetailAdapter extends RecyclerView.Adapter<SelectAddressRetailAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<AddressListBeen.DataBean> mList;
    private Context mContext;
    private int proportion;
    private ShopCarPopupWindow select_address_PW;
    private ConfirmRetailPresenter mPresenter;


    public SelectAddressRetailAdapter(Context mContext, List<AddressListBeen.DataBean> list, ShopCarPopupWindow select_address_PW, ConfirmRetailPresenter mPresenter) {
        this.mList = list;
        this.mContext = mContext;
        this.select_address_PW = select_address_PW;
        this.mPresenter = mPresenter;
    }

    public void setData() {
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_address_pw, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {


        if (mList.get(position).getIs_default() == 1) {
            holder.ivDefaultSelect.setImageResource(R.drawable.icon_select_address_orange);
        } else {
            holder.ivDefaultSelect.setImageResource(R.drawable.only_look_bg_hui);
        }
//        if (mList.get(i).is_chao){
//            iv_default_select.setVisibility(View.GONE);
//            tv_chaochu.setVisibility(View.VISIBLE);
//        }else {
//            iv_default_select.setVisibility(View.VISIBLE);
//            tv_chaochu.setVisibility(View.GONE);
//        }
        holder.tvTypeAddress.setVisibility(View.VISIBLE);
        if (mList.get(position).getLabel() == 1) {
            holder.tvTypeAddress.setText("家");
        } else if (mList.get(position).getLabel() == 2) {
            holder.tvTypeAddress.setText("公司");
        } else if (mList.get(position).getLabel() == 3) {
            holder.tvTypeAddress.setText("学校");
        } else {
            holder.tvTypeAddress.setVisibility(View.GONE);
        }
        holder.tv_address.setText(mList.get(position).getAddress_info());
        holder.tvName.setText(mList.get(position).getRealname());
        holder.tvPhone.setText(mList.get(position).getMobile() + "");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });  holder.ivModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到新增收货地址
                select_address_PW.shareClose();
                Intent intent = new Intent(mContext, AddAddressActivity.class);
                intent.putExtra("from", "confirm");
                intent.putExtra("is_default", mList.get(position).getIs_default()+"");
                intent.putExtra("id", mList.get(position).getId()+"");
                intent.putExtra("data", mList.get(position));
                ((Activity)mContext).startActivityForResult(intent, START_CODE);
            }
        });
        holder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    mPresenter.delectAddress(mList.get(position).getId() + "", position);
                }
            }
        });
    }


    @Override
    public int getItemCount() {

        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_default_select)
        ImageView ivDefaultSelect;//默认的图标
        @BindView(R.id.tv_chaochu)
        TextView tvChaochu;//超出范围
        @BindView(R.id.tv_type_address)
        TextView tvTypeAddress;//地址标签
        @BindView(R.id.tv_address)
        TextView tv_address;//详细地址
        @BindView(R.id.tv_name)
        TextView tvName;//姓名
        @BindView(R.id.tv_phone)
        TextView tvPhone;//手机
        @BindView(R.id.tv_delete)
        TextView tvDelete;//删除
        @BindView(R.id.iv_modify)
        ImageView ivModify;//点击修改图片

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
